package com.yfbao.horizon.shardingtable.injection.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfbao.horizon.shardingtable.injection.entity.User;
import com.yfbao.horizon.shardingtable.injection.service.impl.UserServiceImpl;
import com.yfbao.horizon.shardingtable.injection.service.inter.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
	
	@Autowired
	private UserServiceImpl userService;
	
	@GetMapping("/users")
	public Object list() {
		return userService.list();
	}
	
	@GetMapping("/add")
	public Object add() {
		for (long i = 0; i < 100; i++) {
			User user = new User();
			user.setCity("深圳"+i);
			user.setName("李四"+i);
			userService.save(user);
		}
		return "success";
	}
	
	@GetMapping("/users/{id}")
	public Object get(@PathVariable Long id) {
		return userService.getById(id);
	}
	
	@GetMapping("/users/query")
	public Object get(@RequestParam("name") String name) {
		LambdaQueryWrapper<User> eq = new QueryWrapper<User>().lambda().eq(User::getName, name);
		Page<User> page =new Page<>(1,20);
		return userService.page(page,eq);
	}

	@GetMapping("/users/count")
	public Object getAllCount() {
		return userService.count();
	}


	@GetMapping("/users/like")
	public Object getUserList(@RequestParam("name") String name) {
		return userService.getUserListByNameLike(name);
	}

}
